﻿# qt5 setttings
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
# set(Qt5_DIR "D:\\Qt\\Qt5.10.1\\5.10.1\\mingw53_32\\lib\\cmake\\Qt5")
find_package(Qt5 COMPONENTS Core Widgets Concurrent REQUIRED LinguistTools)
include_directories(${Qt5Widgets_INCLUDE_DIRS})

# translation
if (QT_TS_FILE)
    set_source_files_properties(${QT_TS_FILE} PROPERTIES OUTPUT_LOCATION ${CMAKE_BINARY_DIR})
    add_custom_command(OUTPUT ${QT_TS_FILE} PRE_BUILD
        COMMAND ${Qt5_LUPDATE_EXECUTABLE} -target-language zh_CN ${PROJECT_SOURCE_DIR} -ts ${QT_TS_FILE}
        COMMAND ${Qt5_LRELEASE_EXECUTABLE} ${QT_TS_FILE} -qm ${CMAKE_BINARY_DIR}/${PROJECT_NAME}_zh_CN.qm
    )
endif()

# add include path
include_directories(${PROJECT_BINARY_DIR})

function(QT5_SetTargetProperty target)
    # Remove the console of gui program
    if(WIN32)
        if(MSVC)
            set_target_properties(${target} PROPERTIES
                WIN32_EXECUTABLE YES
                LINK_FLAGS "/ENTRY:mainCRTStartup"
            )
        elseif(CMAKE_COMPILER_IS_GNUCXX)
                SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mwindows") # Not tested
        else()
            message(SEND_ERROR "You are using an unsupported Windows compiler! (Not MSVC or GCC)")
        endif(MSVC)
    elseif(APPLE)
        set_target_properties(${target} PROPERTIES
                MACOSX_BUNDLE YES
        )
    elseif(UNIX)
        # Nothing special required
    else()
        message(SEND_ERROR "You are on an unsupported platform! (Not Win32, Mac OS X or Unix)")
    endif(WIN32)

    # 链接QT库
    target_link_libraries(${target} 
        Qt5::Core Qt5::Widgets Qt5::Concurrent
    )

    # 安装
    get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
    get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
    find_program(DEPLOYQT_EXECUTABLE NAMES windeployqt macdeployqt HINTS "${_qt_bin_dir}")
    file(GLOB COMMON_LIBS ${CMAKE_SOURCE_DIR}/common/bin/*.*)
    install(TARGETS ${target} RUNTIME DESTINATION .)
    install(FILES
        ${CMAKE_BINARY_DIR}/${QT_TS_FILE}_zh_CN.qm
        ${COMMON_LIBS}
        DESTINATION .
    )
    if(WIN32)
        install(CODE "execute_process(
            WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}
            COMMAND ${DEPLOYQT_EXECUTABLE} ${target}.exe -release
        )")
    endif()
endfunction()
